﻿using System;
using System.Collections.Generic;
using System.Linq;
using EzDesk.Core.Data;
using EzDesk.Core.Interface;
using EzDesk.Core.Model;
using EzDesk.Utility.Log;

namespace EzDesk.Core.ServiceImpl.Services
{
    public partial class CoreService : ICoreService
    {
        #region [ private methods ]
        private bool groupHasPermission(Permission permission, Guid groupId)
        {
            return EzDeskDatabaseContainer.Go(
                container =>
                    {
                        bool hasPermission = false;

                        // 默认拒绝为false, 即默认有权限
                        hasPermission = permission.DefaultDeny == false;

                        var permissionGroups =
                            container.Permission_GroupSet
                                .FirstOrDefault(p => p.GroupId == groupId && p.PermissionId == permission.Id);

                        if (permissionGroups != null)
                        {
                            hasPermission = permissionGroups.HasPermission;
                        }

                        return hasPermission;
                    });
        }


        private void setGroupPermission(Permission permission, Guid groupId, bool hasPermission)
        {
            EzDeskDatabaseContainer.GoAndSave(
                container =>
                    {
                        var permissionGroups =
                            container.Permission_GroupSet
                                .FirstOrDefault(p => p.GroupId == groupId && p.PermissionId == permission.Id);

                        if (permissionGroups != null)
                        {
                            permissionGroups.HasPermission = hasPermission;
                        }
                        else
                        {
                            var newPermissionGroup =
                                new Permission_Group()
                                    {
                                        PermissionId = permission.Id,
                                        GroupId = groupId,
                                        HasPermission = hasPermission,
                                    };
                            container.Permission_GroupSet.AddObject(newPermissionGroup);
                        }
                    });
        }
        #endregion


        public void RegisterPermission(Permission permission)
        {
            if (permission == null)
            {
                throw new Exception("权限不存在");
            }
            EzDeskDatabaseContainer.GoAndSave(
                container =>
                {
                    var existPermission = container.PermissionSet
                        .FirstOrDefault(p => p.Name == permission.Name);
                    if (existPermission != null)
                    {
                        throw new Exception("该权限已存在");
                    }

                    container.PermissionSet.AddObject(permission);
                });
        }

        public IList<Permission> GetPermissionList()
        {
            return EzDeskDatabaseContainer.Go(
                container => container.PermissionSet.ToList());
        }

        public Permission GetPermission(string permissionName)
        {
            return EzDeskDatabaseContainer.Go(
                container => container.PermissionSet.FirstOrDefault(p => p.Name == permissionName));
        }

        public bool UserHasPermission(string permissionStr, Guid userId)
        {
            bool hasPermission = false;
            var groups = GetUserInGroups(userId);
            foreach (var group in groups)
            {
                hasPermission = GroupHasPermission(permissionStr, group.Id);
                if (hasPermission)
                {
                    break;
                }
            }
            return hasPermission;
        }

        public bool GroupHasPermission(string permissionName, Guid groupId)
        {
            var permission = EzDeskDatabaseContainer.Go(
                container => container.PermissionSet.FirstOrDefault(p => p.Name == permissionName));

            if (permission == null)
            {
                LogEx.EXCEPT("CoreService - Permission", "权限没找到: " + permissionName);
                return false;
            }

            return groupHasPermission(permission, groupId);
        }

        public void SetGroupPermission(string permissionName, Guid groupId, bool hasPermission)
        {
            var permission = EzDeskDatabaseContainer.Go(
                container => container.PermissionSet.FirstOrDefault(p => p.Name == permissionName));
            
            setGroupPermission(permission, groupId, hasPermission);
        }

    }
}
